package com.jd.myaaaccount.model;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;

import com.jd.myaaaccount.entity.Host;
import com.jd.myaaaccount.entity.User;
import com.jd.myaaaccount.entity.result.CommonNullResult;
import com.jd.myaaaccount.entity.result.GetCategoryResult;
import com.jd.myaaaccount.entity.result.LoginResult;
import com.jd.myaaaccount.entity.result.RegistOrCreateResult;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.HttpHelper;
import com.jd.myaaaccount.util.JsonHelper;
import com.jd.myaaaccount.util.OperateCode;

public class UserModel {
	private Context context;

	public UserModel(Context context) {
		this.context = context;
	}

	public void regist(User user, Handler handler) {
		String params = JsonHelper.getString(user);
		String res = HttpHelper.postRequest("user/regist", params);
		Message msg = new Message();
		msg.what = OperateCode.REGIST;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			RegistOrCreateResult rcr = JsonHelper.getObject(res,
					RegistOrCreateResult.class);
			Common.resultToMessage(rcr, msg);
		}
		handler.sendMessage(msg);
	}

	public void login(User user, Handler handler) {
		String url = String.format("user/login/?email=%s&password=%s",
				user.getEmail(), user.getPassword());
		String res = HttpHelper.getRequest(url);
		Message msg = new Message();
		msg.what = OperateCode.LOGIN;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			LoginResult lr = JsonHelper.getObject(res, LoginResult.class);
			Common.resultToMessage(lr, msg);
			if (lr.getResult() == ErrorCode.SUCCESS) {// 登录成功 信息保存本地
				SharedPreferences sp = context.getSharedPreferences("user",
						Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();
				editor.putString("id", lr.getUser().getId());
				editor.putString("name", lr.getUser().getName());
				editor.putString("token", lr.getUser().getToken());
				editor.putString("email", lr.getUser().getEmail());
				editor.putString("phone", lr.getUser().getPhone());
				editor.putInt("number", lr.getUser().getNumber());
				editor.commit();
				Common.token = lr.getUser().getToken();
				Common.currentUser = new Host(lr.getUser());
			}
		}
		handler.sendMessage(msg);
	}

	public void logout(Handler handler) {
		String url = String.format("user/logout/?userId=%s&token=%s",
				Common.currentUser.getId(), Common.token);
		String res = HttpHelper.getRequest(url);
		Message msg = new Message();
		msg.what = OperateCode.LOGOUT;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			CommonNullResult cnr = JsonHelper.getObject(res,
					CommonNullResult.class);
			Common.resultToMessage(cnr, msg);
			if (cnr.getResult() == ErrorCode.SUCCESS) {// 退出成功 修改本地信息
				Common.cleanLoginInfo((Activity) context);
			}
		}
		handler.sendMessage(msg);
	}

	public void getCategory(String id, Handler handler) {
		String url = String.format("user/getcategory/?userId=%s&token=%s", id,
				Common.token);
		String res = HttpHelper.getRequest(url);
		Message msg = new Message();
		msg.what = OperateCode.GET_CATEGORY;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			GetCategoryResult gcr = JsonHelper.getObject(res,
					GetCategoryResult.class);
			Common.resultToMessage(gcr, msg);
		}
		handler.sendMessage(msg);
	}
}
